import logging
import unittest
import random

import requests

import app
from api.loginAPI import loginAPI
from api.tenderAPI import tenderAPI
from api.trustAPI import trustAPI
from utils import assert_utils, request_third_api, DBUtils


class test_tender_process(unittest.TestCase):
    phone = '16699002222'
    pwd = 'test123'
    imgCode = '8888'
    tender_id = 2167

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = loginAPI()
        cls.trust_api = trustAPI()
        cls.tender_api = tenderAPI()
        cls.session = requests.Session()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()
        sql1 = "delete from mb_member_register_log where phone in ('17688880012','17688880023','17688880034','17688880045','17688880056','16699002222');"
        DBUtils.delete(app.DB_MEMBER,sql1)
        logging.info("delete sql1 = {}".format(sql1))
        sql2 = "delete l.* from mb_member_login_log l INNER JOIN mb_member m on l.member_id = m.id where m.phone in ('17688880012','17688880023','17688880034','17688880045','17688880056','16699002222');"
        DBUtils.delete(app.DB_MEMBER, sql2)
        logging.info("delete sql2 = {}".format(sql2))
        sql3 = "delete i.* from mb_member_info i INNER JOIN mb_member m on i.member_id = m.id where m.phone in ('17688880012','17688880023','17688880034','17688880045','17688880056','16699002222');"
        DBUtils.delete(app.DB_MEMBER, sql3)
        logging.info("delete sql3 = {}".format(sql3))
        sql4 = "delete from mb_member where phone in ('17688880012','17688880023','17688880034','17688880045','17688880056','16699002222');"
        DBUtils.delete(app.DB_MEMBER, sql4)
        logging.info("delete sql4 = {}".format(sql4))

    def test01_registr_success(self):
        # 参数为随机小数时获取图片验证码成功
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)

        # 参数正确,获取短信验证码成功
        response = self.login_api.getSmsCode(self.session, self.phone, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")

        # 注册
        response = self.login_api.register(self.session, self.phone, self.pwd,invite_phone='')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "注册成功")

    def test02_login_success(self):
        response = self.login_api.login(self.session, self.phone, self.pwd)
        logging.info('login resposne = {}'.format(response.json()))
        assert_utils(self, response, 200, 200, "登录成功")

    def test03_trust_success(self):
        response = self.trust_api.trust_register(self.session)
        logging.info("trust_register response = {}".format(response.json()))
        self.assertEqual(200, response.status_code)
        self.assertEqual(200, response.json().get("status"))

        form_data = response.json().get("description").get("form")
        logging.info("form response = {}".format(form_data))
        response = request_third_api(form_data)

        self.assertEqual(200, response.status_code)
        self.assertEqual('UserRegister OK', response.text)


    def test04_rechange_success(self):
        r = random.random()
        response = self.trust_api.get_recharge_verify_code(self.session, str(r))
        self.assertEqual(200, response.status_code)

        response = self.trust_api.recharge(self.session, '1000000')
        logging.info("recharge response = {}".format(response.json()))
        self.assertEqual(200, response.status_code)
        self.assertEqual(200, response.json().get("status"))

        form_data = response.json().get('description').get('form')
        logging.info('form response = {}'.format(form_data))

        response = request_third_api(form_data)
        logging.info("third-interface response={}".format(response.text))

        logging.info('NetSave OK', response.text)

    def test05_get_loaninfo(self):
        response = self.tender_api.get_loaninfo(self.session, self.tender_id)
        logging.info('get_loaninfo response = {}'.format(response.json()))
        assert_utils(self, response, 200, 200, 'OK')
        self.assertEqual(str(self.tender_id), response.json().get('data').get('loan_info').get('id'))

    def test06_tender(self):
        response = self.tender_api.tender(self.session, self.tender_id, '50')
        logging.info('tender response = {}'.format(response.json()))
        self.assertEqual(200, response.status_code)

        form_data = response.json().get('description').get('form')
        logging.info('form response = {}'.format(form_data))

        response = request_third_api(form_data)
        logging.info('third-interface response={}'.format(response.text))
        self.assertEqual('InitiativeTender OK', response.text)

    def test07_get_tender_list(self):
        response = self.tender_api.get_tenderlist(self.session, 'tender')
        logging.info('get_tenderList response = {}'.format(response.json()))
        self.assertEqual(200, response.status_code)
